#!/usr/bin/python
 
from UpCode import file_code
from UpCode import get_file_type
from UpCode import FileDataBase
import re
import os 
import sqlite3


Die = {'picture' :"Pictures",'music' : "Music",'document' : "Documents" ,'movie' = "Movies" }

class NewData(FileDataBase):
    def __init__(self):
        self.cx = sqlite3.connect("/Users/darkh/.programer_type_DB")
        self.cu = self.cx.cussor()
        try:
            self.cu.execute("""create table file_type (id integer primary key ,classification text, type_language text UNIQUE ) """)
            print "hello qingluan"
        except sqlite3.OperationError :
            pass
    def insert_type(self,pro_type):
        count = self.size()
        self.cu.execute('insert into file_type values(%d,"%s","%s") '%(count +1,pro_type[0],pro_type[1]))
        self.cx.commit()  
    def size(self):
        self.cu.execute("select * from file_type")
        type_all = self.cu.fetchall()
        return len(type_all)     
    def get_type(self,type_cla):
        self.cu.execute("select * from file_type   where classification="+type_cla)
        res = self.cu.fetchall()
        self.cu.close()
        self.cx.close()
        return map((lambda x: x[2]),res)

    def close(self):
        if (self.cu ):
            self.cu.close()
            self.cx.close()

class Classification(file_code):
    def __init__(self):
        self.Path = os.getcwd() + "/Downloads/"
        self.DataBase = FileDataBase()
        self.Judege = os.popen("ls ~/Downloads").readlines()
        self.List_all_in_downloads =[]
    def copy_code(self,item):
        if self.Judege:
            pass
        else:
            print "~/Downlaods/ No File you need to classify! Qingluan\n"
            exit(0)
        directory = self.classification(item)
        if directory :
            pass
        else:
            print "No such file!\n"
            exit(0)
        print ("cp -v  " + self.Path + item + "  /Users/darkh/" + directory  + "/")
       # print  ("cp -v  " + self.Desk_path + item + "  /Users/darkh/Dropbox/seed/"+ directory  + "/")
        print("rm   " + self.Path + item )
    def classification(self,item):
        type_item =ge_file_type_without_point(item)
        
        temp_type = [Die[key] for key in Die.keys() ] 
        for ty in temp_type:
            all_type = self.DataBase.get_type(ty)
            for item_type in all_type:
                if type_item in item_type:
                    return ty
    def all_in_Downloads(self):
        self.
    def update(self):
        if self.Judege:
            pass
        else:
            exit(0)
        map(self.copy_code,

def get_file_type_without_point(string):
    compiler = re.compile('(\.[a-z]{1,7})')
    tem = re.findall(compiler,string)
    return tem[-1]



def run():
    cla = Classification()
    cla.copy_code()
